﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace library
{
    public partial class missbook : Form
    {
        public missbook()
        {
            InitializeComponent();
        }
        public string adminId = "";
        public string adminName = "";
        private void missbook_Load(object sender, EventArgs e)
        {
            DataBind_miss();
            int year = System.DateTime.Now.Year;
            int month = System.DateTime.Now.Month;
            int day = System.DateTime.Now.Day;
            missDate_box.Value = new DateTime(year, month, day);
           
        }

        private DataTable SelectSQL(string sql)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            return dt;
        }

        protected void DataBind_miss()
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            string str = "select * from missbookinfo";
            MySqlDataAdapter da = new MySqlDataAdapter(str, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            listView1.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                ListViewItem myitem = new ListViewItem(dr["borrowId"].ToString());
                MySqlConnection c = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
                c.Open();
                string sql = string.Format("select * from borrowinfo where borrowId = {0}", Convert.ToInt32(dr["borrowId"].ToString()));
                MySqlDataAdapter a = new MySqlDataAdapter(sql, c);
                DataTable t = new DataTable();
                a.Fill(t);
                c.Close();
                DataRow r = t.Rows[0];
                myitem.SubItems.Add(r["bookColId"].ToString());
                int readerId = Convert.ToInt32(r["readerId"].ToString());
                //获取读者用户名
                sql = string.Format("select username from readerinfo where readerId = {0}", readerId);
                t = SelectSQL(sql);
                r = t.Rows[0];
                myitem.SubItems.Add(r["username"].ToString());
                myitem.SubItems.Add(dr["amount"].ToString());
                myitem.SubItems.Add(dr["operatTime"].ToString());
                if (dr["pay"].ToString() == "1")
                {
                    myitem.SubItems.Add("已缴纳");
                }
                else
                {
                    myitem.SubItems.Add("未缴纳");
                }
                myitem.SubItems.Add(dr["remark"].ToString());
                string s = string.Format("select * from admininfo where adminId = {0}", dr["adminId"].ToString());
                DataTable d = SelectSQL(s);
                r = d.Rows[0];
                myitem.SubItems.Add(r["name"].ToString());
                listView1.Items.Add(myitem); //将新建项添加到 ListView 控件中
            }

        }

        private void bookColId_box_TextChanged(object sender, EventArgs e)
        {

        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            ClearTexBox();
        }

        protected void ClearTexBox()
        {
            username_box.Text = "";
            bookColId_box.Text = "";
            int year = System.DateTime.Now.Year;
            int month = System.DateTime.Now.Month;
            int day = System.DateTime.Now.Day;
            missDate_box.Value = new DateTime(year, month, day);

        }

        private void return_btn_Click(object sender, EventArgs e)
        {
            this.Hide();
            Main frm = new Main();
            frm.Show();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string username = username_box.Text.Trim();
            string bookCol = bookColId_box.Text.Trim();
            if (username == "")
            {
                MessageBox.Show("请输入读者用户名");
                return;
            }
            if (bookCol == "")
            {
                MessageBox.Show("请输入馆藏编号");
                return;
            }
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            conn.Open();
            string sql = string.Format("select * from readerinfo where username = '{0}'", username);
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            conn.Close();
            DataRow dr = dt.Rows[0];
            string readerId = dr["readerId"].ToString();
            string loss = dr["loss"].ToString();
            if (loss == "1")
            {
                MessageBox.Show("该账户以挂失，不可以办理书本遗失");
                return;
            }
            
            //正常办理书本遗失
            bool falg = missBook(bookCol, readerId, missDate_box.Value.ToString("yyyy-MM-dd"), Convert.ToInt32(adminId));
            if (falg)
            {
                MessageBox.Show("办理成功");
                DataBind_miss();
                ClearTexBox();
            }
            else
            {
                MessageBox.Show("办理失败");
            }
        }

        protected bool missBook(string ColId, string readId, string reDate, int adId)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost; database=librarysystem; UID=root; PWD = 123456; port = 3306");
            MySqlCommand cmd = new MySqlCommand("missBook", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            MySqlParameter[] parames =
            {
                new MySqlParameter("ColId",ColId),
                new MySqlParameter("readId",readId),
                new MySqlParameter("reDate",reDate),
                new MySqlParameter("adId",adId),
            };
            cmd.Parameters.AddRange(parames);
            conn.Open();
            Object obj = cmd.ExecuteScalar();
            conn.Close();
            if (obj.ToString().Equals("0"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
